
/*---------------------------------------------------------
   If the entity is colliding with something this function
   will make it non solid and call a timer to itself.
   
   If it isn't colliding with anything it will make it solid
   and the timer will end.
---------------------------------------------------------*/
function CheckPropSolid( e, solidtype, nonsolidtype, SolidCallback )

	if ( e == NULL ) then return end

	// Trace our entity to check for collisions
	local trace = { start = e:GetPos(), endpos = e:GetPos(), filter = e }
	local tr = util.TraceEntity( trace, e ) 
	
	// If it collides mark it non solid and test again in 0.5 seconds
	if ( tr.Hit ) then
	
		e:SetCollisionGroup( nonsolidtype )
		
		// Check later and later every time.
		//timerlast = timerlast + 0.1
		timer.Simple( 0.5, CheckPropSolid, e, solidtype, nonsolidtype, SolidCallback )
		
	else
	
		e:SetCollisionGroup( solidtype )
		
		// If we have a solid callback function call it
		if ( SolidCallback ) then
			SolidCallback( e )
		end
	
	end

end


function DoPropSpawnedEffect( e )

	local ed = EffectData()
		ed:SetEntity( e )
	util.Effect( "propspawn", ed, true, true )

end

function Magnet( pl, pos, angle, model, material, key, maxobjects, strength, nopull, allowrot, alwayson, toggle, Vel, aVel, frozen )
	if ( !gamemode.Call( "PlayerSpawnMagnet", pl, Model ) ) then return end
	local magnet = ents.Create("phys_magnet")
	magnet:SetPos(pos)
	magnet:SetAngles(angle)
	magnet:SetModel( model )
	magnet:SetMaterial( material )
				
	local spawnflags = 4
	if (nopull > 0) then		spawnflags = spawnflags - 4		end		// no pull required: remove the suck flag
	if (allowrot > 0) then		spawnflags = spawnflags + 8		end
				
	magnet:SetKeyValue( "maxobjects", maxobjects )
	magnet:SetKeyValue( "forcelimit", strength )
	magnet:SetKeyValue( "spawnflags", spawnflags )
	magnet:SetKeyValue( "overridescript", "surfaceprop,metal")
	magnet:SetKeyValue( "massScale", 0 )
	
	magnet:Activate()
	magnet:Spawn()
	
	if magnet:GetPhysicsObject():IsValid() then
		Phys = magnet:GetPhysicsObject()
		if Vel then Phys:SetVelocity(Vel) end
		if Vel then Phys:AddAngleVelocity(aVel) end
		Phys:EnableMotion(frozen != true)
	end

	if (alwayson > 0) then
		magnet:Input("TurnOn", nil, nil, nil)
	else
		magnet:Input("TurnOff", nil, nil, nil)
	end
	
	local mtable = {
		model = model,
		material = material,
		key = key,
		maxobjects = maxobjects,
		strength = strength,
		nopull = nopull,
		allowrot = allowrot,
		alwayson = alwayson,
		toggle = toggle
	}
	
	magnet:SetTable( mtable )
	
	numpad.OnDown( 	 pl, 	key, 	"MagnetOn", 	magnet )
	numpad.OnUp( 	 pl, 	key, 	"MagnetOff", 	magnet )
	
	gamemode.Call( "PlayerSpawnedMagnet", pl, model, magnet )
	return magnet
	
end
duplicator.RegisterEntityClass( "phys_magnet", Magnet, "pos", "angle", "model", "material", "key", "maxobjects", "strength", "nopull", "allowrot", "alwayson", "toggle", "Vel", "aVel", "frozen" )
CreateConVar('sbox_maxmagnets', 50)